//
//  Color.swift
//  laiyifen
//
//  Created by 朱献国 on 2021/10/29.
//  Copyright © 2021 laiyifen. All rights reserved.
//

import UIKit

extension UIColor: UIColorConverter {
    /// #FF6900
    class var FF6900: UIColor { colorWith(hexString: "#FF6900") }
    
    /// #FEFFFE
    class var FEFFFE: UIColor { colorWith(hexString: "#FEFFFE") }
    
    /// #FD6800
    class var FD6800: UIColor { colorWith(hexString: "#FD6800") }
    
    /// #C4C4C4
    class var C4C4C4: UIColor { colorWith(hexString: "#C4C4C4") }
    
    /// #999999
    class var C999999: UIColor { colorWith(hexString: "#999999") }

    /// #666666
    class var C666666: UIColor { colorWith(hexString: "#666666") }
    
    /// #333333
    class var C333333: UIColor { colorWith(hexString: "#333333") }
    
    /// #000000
    class var C000000: UIColor { colorWith(hexString: "#000000") }
    
    /// #FFFFFF
    class var FFFFFF: UIColor { colorWith(hexString: "#FFFFFF") }
    
    /// #FAFAFA
    class var FAFAFA: UIColor { colorWith(hexString: "#FAFAFA") }
    
    /// #FF3B30
    class var FF3B30: UIColor { colorWith(hexString: "#FF3B30") }
    
    /// #F5F5F5
    class var F5F5F5: UIColor { colorWith(hexString: "#F5F5F5") }
    
    /// #FFF5E6
    class var FFF5E6: UIColor { colorWith(hexString: "#FFF5E6") }
    
    /// #FFF1E6
    class var FFF1E6: UIColor { colorWith(hexString: "#FFF1E6") }
    
    /// #E5E5E5
    class var E5E5E5: UIColor { colorWith(hexString: "#E5E5E5") }
    
    /// #DBB177
    class var DBB177: UIColor { colorWith(hexString: "#DBB177") }
    
    /// #C7A97A
    class var C7A97A: UIColor { colorWith(hexString: "#C7A97A") }
    
    /// #F7E7CB
    class var F7E7CB: UIColor { colorWith(hexString: "#F7E7CB") }
    
    /// #F3E3CB
    class var F3E3CB: UIColor { colorWith(hexString: "#F3E3CB") }
    
    /// #1F242D
    class var C1F242D: UIColor { colorWith(hexString: "#1F242D") }
    
    /// #FE0500
    class var FE0500: UIColor { colorWith(hexString: "#FE0500") }
    
    /// #EAEAEA
    class var EAEAEA: UIColor { colorWith(hexString: "#EAEAEA") }
    /// #CCCCCC
    class var CCCCCC: UIColor { colorWith(hexString: "#CCCCCC") }
    
    /// #D5540F
    class var D5540F: UIColor { colorWith(hexString: "#D5540F") }
    
    /// #353535
    class var C353535: UIColor { colorWith(hexString: "#353535") }
    
    /// #E8C772
    class var E8C772: UIColor { colorWith(hexString: "#E8C772") }
    
    /// #C272727
    class var C272727: UIColor { colorWith(hexString: "#272727") }
    
    
    /// 随机色
    class var random: UIColor {
        UIColor(red: CGFloat(arc4random() % 256) / 255.0,
                green: CGFloat(arc4random() % 256) / 255.0,
                blue: CGFloat(arc4random() % 256) / 255.0,
                alpha: 1.0)
    }
}

protocol UIColorConverter {}

extension UIColorConverter {
    /// 十六进制字符串获取颜色
    /// - Parameter hexString: 颜色16进制字符串
    /// - Returns: UIColor
    static func colorWith(hexString: String) -> UIColor {
        let color = rgbColor(from: hexString)
        guard let r = color.red, let g = color.green, let b = color.blue, let a = color.alpha else {
            assert(false, "十六进制颜色值有误")
            return .white
        }
        return UIColor(red: r / CGFloat(255), green: g / CGFloat(255), blue: b / CGFloat(255), alpha: a)
    }
    
    /// 根据 十六进制字符串颜色获取 RGB值
    /// - Parameter hexString: 颜色16进制字符串
    /// - Returns: RGB值
    static func rgbColor(from hexString: String) -> (red: CGFloat?, green: CGFloat?, blue: CGFloat?, alpha: CGFloat?) {
        // 2、将字符串转成大写
        var tmpHexString = hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        
        // 3、去除开头 0x/#/##
        if tmpHexString.hasPrefix("0x") ||
            tmpHexString.hasPrefix("##") {
            tmpHexString = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 2) ..<
                    tmpHexString.endIndex])
        }
        if tmpHexString.hasPrefix("#") {
            tmpHexString = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 1) ..<
                    tmpHexString.endIndex])
        }
        
        // 4、判断字符串的长度是否符合
        // RGB            RGBA          RRGGBB        RRGGBBAA
        guard tmpHexString.count == 3 ||
            tmpHexString.count == 4 ||
            tmpHexString.count == 6 ||
            tmpHexString.count == 8
        else {
            return (nil, nil, nil, nil)
        }
        
        // 5、RGB,RGBA,RRGGBB,RRGGBBAA
        var r: UInt32 = 0, g: UInt32 = 0, b: UInt32 = 0, a: CGFloat = 0
        if tmpHexString.count < 5 {
            let rHex = String(tmpHexString[tmpHexString.startIndex ..<
                    tmpHexString.index(tmpHexString.startIndex, offsetBy: 1)])
            let gHex = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 1) ..<
                    tmpHexString.index(tmpHexString.startIndex, offsetBy: 2)])
            let bHex = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 2) ..<
                    tmpHexString.index(tmpHexString.startIndex, offsetBy: 3)])
            
            if hexString.count == 4 {
                var tempA: UInt32 = 0
                let aHex = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 3) ..<
                        tmpHexString.index(tmpHexString.startIndex, offsetBy: 4)])
                Scanner(string: aHex).scanHexInt32(&tempA)
                a = CGFloat(tempA) / CGFloat(255)
            } else {
                a = 1
            }
            Scanner(string: rHex).scanHexInt32(&r)
            Scanner(string: gHex).scanHexInt32(&g)
            Scanner(string: bHex).scanHexInt32(&b)
        } else {
            let rHex = String(tmpHexString[tmpHexString.startIndex ..<
                    tmpHexString.index(tmpHexString.startIndex, offsetBy: 2)])
            let gHex = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 2) ..<
                    tmpHexString.index(tmpHexString.startIndex, offsetBy: 4)])
            let bHex = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 4) ..<
                    tmpHexString.index(tmpHexString.startIndex, offsetBy: 6)])
            
            if hexString.count == 8 {
                var tempA: UInt32 = 0
                let aHex = String(tmpHexString[tmpHexString.index(tmpHexString.startIndex, offsetBy: 6) ..<
                        tmpHexString.index(tmpHexString.startIndex, offsetBy: 8)])
                Scanner(string: aHex).scanHexInt32(&tempA)
                a = CGFloat(tempA) / CGFloat(255)
            } else {
                a = 1
            }
            Scanner(string: rHex).scanHexInt32(&r)
            Scanner(string: gHex).scanHexInt32(&g)
            Scanner(string: bHex).scanHexInt32(&b)
        }
        
        return (red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), a)
    }
}
